package edu.towson.cosc.cosc455.nbedard.markdowncompiler;

import java.io.IOException;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;
import java.lang.String;
public class MyLexicalAnalyzer implements LexicalAnalyzer {

	/** The next character. */
    static char nextCharacter;

    /** The current position. */
    int currentPosition = 0;
	
    public void parse(){
		MySyntaxAnalyzer.parseTree.add(MyCompilerClass.token);
		System.out.println("parsed.");
    }
    
	@Override
	public void getNextToken() {
		MyCompilerClass.token = "";
		nextCharacter = ' ';
		getCharacter();
		parse();
	}
	
	@Override
	public void getCharacter() {
		try {
			if(MyCompilerClass.input.ready()){
			try {
				nextCharacter = (char) MyCompilerClass.input.read();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("token " + MyCompilerClass.token);
			
			if(nextCharacter=='#'){
				parse();
				MyCompilerClass.token = "";
			}
			if(isSpace(nextCharacter)==false || lookupToken()==false){
				if(isSpace(nextCharacter)==true && 
						(MyCompilerClass.token.toLowerCase().startsWith("#start ") ||
						MyCompilerClass.token.toLowerCase().startsWith("#end "))&&
						lookupToken()==false){
					System.err.println(MyCompilerClass.token + " is not a valid markup lexeme.");
					System.exit(0);
				}
				MyCompilerClass.token += nextCharacter;
				currentPosition++;
				getCharacter();
			}
			
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void addCharacter() {}

	@Override
	public boolean isSpace(char c) {
		if(Character.isWhitespace(c) || c==' ')return true;
		return false;
	}

	@Override
	public boolean lookupToken() {
		if(MyCompilerClass.token.toLowerCase().startsWith("#start")){
			if(MyCompilerClass.token.toLowerCase().endsWith("document")) return true; 
			if(MyCompilerClass.token.toLowerCase().endsWith("head")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("title")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("paragraph")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("bold")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("italics")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("list")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("item")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("item")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("audio")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("video")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("define")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("use")) return true;
		}
		if(MyCompilerClass.token.toLowerCase().startsWith("#end")){
			if(MyCompilerClass.token.toLowerCase().endsWith("document")) return true; 
			if(MyCompilerClass.token.toLowerCase().endsWith("head")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("title")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("paragraph")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("bold")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("italics")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("list")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("item")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("item")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("audio")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("video")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("define")) return true;
			if(MyCompilerClass.token.toLowerCase().endsWith("use")) return true;
		}
		if(MyCompilerClass.token.equalsIgnoreCase("#newline")) return true;
		if(MyCompilerClass.token.equalsIgnoreCase("#name")) return true;
		if(MyCompilerClass.token.equalsIgnoreCase("#value")) return true;
		return false;
	}

}
